let vm = new Vue({
    el: "#app",
    data: {
        total: 0, // 购物车金额
        count: 0, // 购物车数量
        freight: 10, // 运费
        cart_id: "", // 购物车id
        isLogin: false, // 是否登录
        order: { // 订单信息
            payStyle: "0", // 支付方式
            freight: 10, // 运费
            distId: "", // 配送id
            total: 0 // 订单总金额
        },
        user: {}, // 用户信息
        recs: [], // 收货信息
        carts: [] // 购物车
    },
    created: function () {
        let cart_id = location.search.split("=")[1];

        if (cart_id !== undefined) {
            this.cart_id = cart_id;

            // 通过id查询购物车
            axios.get("/shop_cart/selectById", {
                params: {
                    id: cart_id
                }
            }).then(function (response) {
                vm.count = 0;
                vm.total = 0;
                vm.carts.push(response.data);

                console.log(response.data);

                for (let i = 0; i < vm.carts.length; i++) {
                    let cart = vm.carts[i];

                    vm.count += cart.quantity;
                    vm.total += cart.money;
                }
            });
        } else {
            // 查询用户的购物车
            axios.get("/shop_cart/select").then(function (response) {
                vm.count = 0;
                vm.total = 0;
                vm.carts = response.data;

                for (let i = 0; i < vm.carts.length; i++) {
                    let cart = vm.carts[i];

                    vm.count += cart.quantity;
                    vm.total += cart.money;
                }
            });
        }

        // 获取登录的用户信息
        axios.get("/user/getlogin").then(function (response) {
            vm.user = response.data;
            vm.isLogin = response.data !== "";
        });

        // 查询用户的收货信息
        axios.get("/receive_info/selectByUserId").then(function (response) {
            vm.recs = response.data;
        });
    },
    methods: {
        logout() { // 退出登录
            axios.get("/user/logout").then(function (response) {
                if (response.data.code === 200) {
                    location.href = "/login.html";
                } else {
                    alert(response.data.msg);
                }
            });
        },
        submit() { // 提交订单
            if (vm.order.distId === "") {
                vm.$message({
                    showClose: true,
                    message: '请选择收货信息'
                });
            } else if (vm.order.payStyle === "") {
                vm.$message({
                    showClose: true,
                    message: '请选择支付方式'
                });
            } else {
                // 请求路径
                let requestUrl = "/order/generate";

                // 如果地址栏传过来了购物车id，只添加一条购物车
                if (vm.cart_id !== "") {
                    requestUrl = "/order/insert";
                }

                vm.order.total = vm.total;

                axios.post(requestUrl, vm.order).then(function (response) {
                    if (response.data.code === 200) {
                        $('.popup_con').fadeIn('fast', function () {
                            setTimeout(function () {
                                $('.popup_con').fadeOut('fast', function () {
                                    vm.$notify({
                                        title: '成功',
                                        type: 'success',
                                        message: response.data.msg
                                    });

                                    location.href = "/html/user_center.html?index=1";
                                });
                            }, 1000)
                        });
                    } else {
                        vm.$message({
                            type: 'error',
                            showClose: true,
                            message: response.data.msg
                        });
                    }
                });
            }
        }
    }
});